import { FunctionStore } from './FunctionStore';

class Command {
    private pipe: Map<string, number> = new Map<string, number>();

    private fnStore = new FunctionStore();

    add(name: string, func: Function) {
        this.fnStore.add(name, func);
        while (this.pipe.has(name) && this.pipe.get(name) > 0) {
            const n = this.pipe.get(name) ?? 0;
            this.pipe.set(name, n - 1);
            this.fnStore.call(name);
        }
    }

    execute(name: string) {
        if (this.fnStore.get(name)) {
            return this.fnStore.call(name);
        } else {
            let n = this.pipe.get(name) ?? 0;
            this.pipe.set(name, n + 1);
            return null;
        }
    }
}

// export function getCommand() {
//     if (!globalThis.command) {
//         globalThis.command = new Command();
//         console.log('instance');
//     }
//     return globalThis.command;
// }
// export const command = getCommand();
